/*
 * @Title: 姿美汇H5商城 - 认领店主
 * @Author: JSL
 * @Date:   2018-10-31
 * @Last Modified by:  JSL
 * @Last Modified time: 2018-10-31 16:15:41
 */
var dataInfo = new Object(),
	template = {
		init: function() {
			dataInfo.init = new Vue({
				el: '#app',
				data: {
					hasStore: 0, //是否是店主 初始化-0 第一次是-1 已经是-2
					getCodeInfo: '获取验证码',
					warringInfo: '',
					orderNum: '',
					phone: '',
					code: '',
					isYY: 1,
					codeNum: 1,
					statusSwitch: 1,
				},
				methods: {
					postUrl: function() {
						dataInfo.locationUrl = window.location.href;
						if(dataInfo.locationUrl.indexOf('//testmtmall.meetao.com/') > 0) { //开发环境
							addressCodeUrl = 'https://testoauth.meetao.com/';
						} else if(dataInfo.locationUrl.indexOf('//premtmall.meetao.com/') > 0) { //预生产
							addressCodeUrl = 'https://preoauth.meetao.com/';
						} else if(dataInfo.locationUrl.indexOf("//graymtmall.meetao.com/") > 0) { //灰度
							addressCodeUrl = 'https://grayoauth.meetao.com/';
						} else if(dataInfo.locationUrl.indexOf("//mtmall.meetao.com/") > 0) { //生产
							addressCodeUrl = 'https://oauth.meetao.com/';
						} else { //本地测试环境
							addressCodeUrl = 'https://testoauth.meetao.com/';
						};
					},
					loginClose: function() {
						this.hasStore = 0;
					},
					submit: function() { //H5分销登录
						if(!this.orderNum) {
							this.$dialog.alert({
								title: '请输入天猫订单号/收货人手机号'
							});
						} else if(!this.phone) {
							this.$dialog.alert({
								title: '请输入姿美汇注册手机号'
							});
						} else if(!(/^1[0-9]\d{9}$/.test(this.phone))) {
							this.$dialog.alert({
								title: '请输入正确手机号'
							});
						} else if(!this.code) {
							this.$dialog.alert({
								title: '请输入验证码'
							});
						} else if(this.code.length != 6) {
							this.$dialog.alert({
								title: '请输入正确的验证码'
							});
						} else if(!!this.active && this.statusSwitch == 1) {
							this.statusSwitch = 2;
							ajaxTemplate(addressCodeUrl + SERVICE.rldz, function(data) {
								if(data.tag == 0) {
									dataInfo.init.hasStore = 1;
								} else {
									if(data.data.errCode == 2007) {
										dataInfo.init.hasStore = 2;
									} else {
										dataInfo.init.$dialog.alert({
											title: data.data.errMsg
										});
									};
								};
								dataInfo.init.statusSwitch = 1;
							}, {
								phone: dataInfo.init.phone,
								verifyCode: dataInfo.init.code,
								type: 1,
								crmParms: dataInfo.init.orderNum,
								pt_: 'rldz'
							}, {
								nologin: 1
							});
						};
					},
					getYYCode: function() { //获取语音验证码
						if(this.codeNum == 2) {
							this.$dialog.alert({
								title: '正在获取短信验证码，请稍后'
							});
						} else if(!this.orderNum) {
							this.$dialog.alert({
								title: '请输入天猫订单号/收货人手机号'
							});
						} else if(!(/^1[0-9]\d{9}$/.test(this.phone))) {
							this.$dialog.alert({
								title: '请输入正确手机号'
							});
						} else {
							dataInfo.init.$dialog.confirm({
								title: '确认接听语音验证码'
							}).then(() => {
								dataInfo.init.codeNum == 2;
								dataInfo.init.isYY = 0;
								dataInfo.init.getCoding();
							});
						};
					},
					getCode: function() { //获取短信验证码
						if(dataInfo.init.codeNum == 1) { //发送短信 - start
							if(!this.orderNum) {
								this.$dialog.alert({
									title: '请输入天猫订单号/收货人手机号'
								});
							} else if(!(/^1[0-9]\d{9}$/.test(this.phone))) {
								this.$dialog.alert({
									title: '请输入正确手机号'
								});
							} else {
								this.codeNum == 2;
								this.isYY = 1;
								dataInfo.init.getCodeInfo = '发送中..';
								dataInfo.init.getCoding();
							};
						}; //发送短信 - end
					},
					getCoding: function() { //  dataInfo.init.getCoding();
						dataInfo.timestamp = new Date().getTime().toString();
						dataInfo.phone = '';
						dataInfo.init.phone.split('').forEach(function(item, index) {
							index == 0 || index == 2 || index == 4 || index == 6 || index == 8 ? dataInfo.phone += item : '';
						});
						dataInfo.phoneNum = hex_md5(hex_md5(dataInfo.init.phone.toString()) + (dataInfo.phone + dataInfo.timestamp).toString());
						ajaxTemplate(addressCodeUrl + SERVICE.rlCode, function(data) {
							if(data.tag == 0) {
								dataInfo.dataTime = 60;
								dataInfo.times = setInterval(function() {
									if(dataInfo.dataTime <= 1) {
										dataInfo.init.getCodeInfo = '重新发送';
										dataInfo.times = window.clearInterval(dataInfo.times);
										dataInfo.init.codeNum = 1;
									} else {
										dataInfo.init.codeNum = 2;
										dataInfo.init.getCodeInfo = --dataInfo.dataTime + 's';
									}
								}, 1000);
							} else {
								dataInfo.init.codeNum = 1;
								dataInfo.init.getCodeInfo = '重新发送';
								dataInfo.init.$dialog.alert({
									title: data.errMsg
								});
							};
						}, {
							phone: dataInfo.init.phone,
							code: dataInfo.timestamp,
							sign: dataInfo.phoneNum,
							msgType: 'rldz',
							crmParms: dataInfo.init.orderNum,
							isYY: dataInfo.init.isYY
						}, {
							nologin: 1
						});
					},
					downLoad: function() {
						this.hasStore == 1 ? downAppFun() : this.hasStore = 0;
					},
				},
				computed: {
					active: function() {
						return !!/^1[0-9]\d{9}$/.test(this.phone) && this.code.length == 6;
					}
				},
			});
			dataInfo.init.postUrl();
		}
	};
template.init();
